package com.baizhi.action;

import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.UUID;

public class UserAction {
    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    private User u;
    private String username;
    private String password;
    private File cover;//图片头像文件
    private String coverFileName;//图片头像文件名字  要求图片文件名字必须以该图片文件的名字+FileName构成
    private String safeCode;//验证码

    public String getSafeCode() {
        return safeCode;
    }

    public void setSafeCode(String safeCode) {
        this.safeCode = safeCode;
    }

    public File getCover() {
        return cover;
    }

    public void setCover(File cover) {
        this.cover = cover;
    }

    public String getCoverFileName() {
        return coverFileName;
    }

    public void setCoverFileName(String coverFileName) {
        this.coverFileName = coverFileName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User getU() {
        return u;
    }

    public void setU(User u) {
        this.u = u;
    }
    public String register(){
        HttpServletRequest request = ServletActionContext.getRequest();
        ServletContext servletContext = request.getServletContext();
        String realPath = servletContext.getRealPath("/img");
        String realFileName = UUID.randomUUID().toString()+coverFileName;
        System.out.println("图片文件名："+coverFileName);
        InputStream is = null;
        OutputStream os = null;

        try {
            System.out.println(cover);
            is = new FileInputStream(cover);
            os = new FileOutputStream(realPath+"/"+realFileName);
            while (true){
                int i = is.read();
                if(i==-1) break;
                os.write(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        u.setHeadpic(realFileName);
        int i = userService.insertUser(u);
        if(i>0){
            return "registOk";
        }else{
            return "registError";
        }
    }
    public String login(){
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        String realcode = (String) session.getAttribute("validatecode");
        if(!realcode.equalsIgnoreCase(safeCode)){
            //验证码失败
            return "loginError";
        }
        u = userService.selectByUsername(username);
        if(u!=null && u.getPassword().equals(password)){
            System.out.println("登陆成功后的用户"+u);
            session.setAttribute("login",u);
            return "success";
        }else{
            return "loginError";
        }
    }
    public String exit(){
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        session.removeAttribute("login");
        return "loginError";
    }
}
